//
//  GoodsMoreView.m
//  wedding
//
//  Created by 常宽 on 15/1/30.
//  Copyright (c) 2015年 常宽. All rights reserved.
//

#import "GoodsMoreView.h"
#import "TypeModel.h"   /// 
#import "GoodsModel.h"  /// 商品类
#import "PVGoodsCell.h" /// 商品的cell

@interface GoodsMoreView ()<UITableViewDataSource, UITableViewDelegate, PullTableViewDelegate>
@property (nonatomic, retain)CKTableView *tableView;
/// 数据数组
@property (nonatomic, retain)NSMutableArray *dataArray;
@end

@implementation GoodsMoreView

- (instancetype) initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.model = [[TypeModel alloc] init];
        self.dataArray = [[NSMutableArray alloc]init];
//        [self netWorking];
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    [self netWorking];

    [self createTableView];
}
- (void)netWorking
{
    // 参数category :类型id, page:页码, sort:排序(未实现, 0=默认,1=喜欢最多,2=最新发布,3=价格最低,4=价格最高)
    NSString *url = [NSString stringWithFormat:@"http://hunliji.com/products.json?page=1&user_id=(null)&category=%@&sort=0", self.model.idNumder];
    [NetHandler networkWithURL:url completion:^(id result) {
        NSArray *array = [result objectForKey:@"products"];
        for (NSDictionary *dic in array) {
            GoodsModel *model = [[GoodsModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.dataArray addObject:model];
            NSLog(@"======%@", model.idNumder);
        }
        [self.tableView reloadData];
    }];
}

- (void)createTableView
{
    self.tableView = [[CKTableView alloc]initWithFrame:self.bounds style:(UITableViewStylePlain)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.pullDelegate = self;
    [self addSubview:self.tableView];
}
#pragma mark 加载刷新
- (void) refreshTable
{
    [self.tableView reloadData];
    
    self.tableView.pullLastRefreshDate = [NSDate date];
    self.tableView.pullTableIsRefreshing = NO;
}

- (void) loadMoreDataToTable
{
    //    //  加载更多实现代码放在在这
    [self shuaxinNetWork];
    self.tableView.pullTableIsLoadingMore = NO;
}
/// 加载更多
///  刷新方法
static NSInteger count = 2;
- (void)shuaxinNetWork
{
    NSString *strUrl = [NSString stringWithFormat:@"http://hunliji.com/products.json?page=%ld&user_id=(null)&category=%@&sort=0",count ,self.model.idNumder];
    [NetHandler networkWithURL:strUrl completion:^(id result) {
        NSArray *array = [result objectForKey:@"products"];
        for (NSDictionary *dic in array) {
            GoodsModel *model = [[GoodsModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.dataArray addObject:model];
        }
        [self.tableView reloadData];
    }];
    count ++;
}
#pragma mark -
#pragma mark - PullTableViewDelegate协议

- (void)pullTableViewDidTriggerRefresh:(PullTableView *)pullTableView
{
    [self performSelector:@selector(refreshTable) withObject:nil afterDelay:3.0f];
}

- (void)pullTableViewDidTriggerLoadMore:(PullTableView *)pullTableView
{
    [self performSelector:@selector(loadMoreDataToTable) withObject:nil afterDelay:3.0f];
}

#pragma mark -

#pragma mark -
#pragma mark tabelView协议
///  section 个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
///  每个section里的row的个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}
///  返回tableView里的cell样式
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *string = @"goodsMore";
//    NSString *string = [NSString stringWithFormat:@"moreGoods%@", self.model.name];

    PVGoodsCell *cell = [tableView dequeueReusableCellWithIdentifier:string];
    if (!cell) {
        cell = [[PVGoodsCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:string];
    }
    cell.model = [self.dataArray objectAtIndex:indexPath.row];
    return cell;
}
/// 返回row的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    GoodsModel *model = [self.dataArray objectAtIndex:indexPath.row];
    float w = [model.width floatValue];
    float h = [model.height floatValue];
    float H = (tableView.frame.size.width / w) * h;
    return H;
}
///  选中cell执行的方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    self.block(self.dataArray[indexPath.row]);
}

#pragma mark -


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
